package com.global.gbinventory.main.model;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class GBTableModel extends AbstractTableModel{

	private static final long serialVersionUID = 8585924307661915002L;
	
	protected String[] columnNames;
	protected List<Object[]> data;
	
	public GBTableModel(){
		data = new ArrayList<Object[]>();
		columnNames = new String[5];
	}
	
	@Override
	public int getRowCount() {
		int rows = 0;
		if (data != null) {
			rows = data.size();
		}
		return rows;
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data.get(rowIndex)[columnIndex];
	}
	
	public void setColumnNames(String[] columnNames){
		this.columnNames = columnNames;
	}
	
	public String[] getColumnNames(){
		return this.columnNames;
	}
	
	public void setData(List<Object[]> data){
		this.data = data;
	}

	public List<Object[]> getData(){
		return this.data;
	}
	
	public Object[] getTuple(int row){
		return this.data.get(row);
	}
	
	public void setTuple(int row, Object[] tuple){
		this.data.set(row, tuple);
		fireTableRowsUpdated(row, row);
	}
	
	public String getColumnName(int col) {
        return columnNames[col];
    }
	
	public List<Object> getColumnObjects(int col){
		List<Object> cols = new ArrayList<Object>();
		for(Object[] tuple : this.data){
			Object obj = tuple[col];
			cols.add(obj);
		}
		
		return cols;
	}

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {
        data.get(row)[col] = value;
        fireTableCellUpdated(row, col);
    }
    
    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
//    public Class getColumnClass(int c) {
//        return getValueAt(0, c).getClass();
//    }

    protected boolean addRow(Object[] tuple) {
		boolean success = false;
		
		data.add(tuple);
		success = true;
		
		fireTableRowsInserted(data.size() - 1, data.size() - 1);
		return success;
		
	}
    
    protected boolean removeRow(int row){
    	boolean success = false;
    	
    	data.remove(row);
    	success = true;
    	
    	fireTableRowsDeleted(row, row);
    	return success;
    }
    
    //mesti diperiksa lagi
    protected boolean isEmptyTuple(int row){
    	Object[] tuple = getTuple(row);
    	for(Object obj : tuple){
    		if(!(obj.equals(""))){
    			return false;
    		}
    	}
    	return true;
    }

}
